<template>
  <div class="create-page">
    <div class="left-slide">
      <StructureList @nodeClick="nodeClick" />
    </div>
    <div class="right-form-container">
      <!-- <WorkOrderTable :data-type="0" /> -->
      <!-- 操作栏 -->
      <div class="header-box">
        <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="关键词" prop="keyword">
            <el-input v-model="searchForm.keyword" placeholder="请输入关键词" />
          </el-form-item>
          <!-- <el-form-item label="状态" prop="status">
            <el-select v-model="searchForm.status">
              <el-option v-for="item in statusArr" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item> -->
          <el-form-item>
            <el-button type="primary" @click="search('searchForm')">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="info" @click="reset('searchForm')">重置</el-button>
          </el-form-item>
        </el-form>
        <el-button type="primary" @click="$refs.AddUser.add() && ($refs.AddUser.title = '新增用户')"> <i class="el-icon-plus" /> 新增用户</el-button>
      </div>
      <div class="table-container">
        <div class="table-box">
          <el-table
            :data="contentData"
            border
            :header-cell-style="{textAlign:'center'}"
            :cell-style="{textAlign:'center'}"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" fixed />
            <el-table-column prop="account" label="账号" />
            <el-table-column prop="name" label="姓名" />
            <el-table-column prop="sex" label="性别">
              <template slot-scope="scope">
                {{ scope.row.sex === 1 ? '男' : '女' }}
              </template>
            </el-table-column>
            <el-table-column prop="phone" label="手机" />
            <el-table-column prop="createTime" label="创建时间" />
            <!-- <el-table-column prop="status" label="状态" /> -->
            <el-table-column label="操作" width="240" fixed="right">
              <template slot-scope="scope">
                <a @click="edit(scope.row)"><i class="el-icon-edit" /> 编辑</a>
                <el-divider direction="vertical" content-position="center" />
                <a>
                  <el-dropdown @command="clickCommand($event, scope.row)">
                    <span class="el-dropdown-link">
                      更多<i class="el-icon-arrow-down el-icon--right" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item command="修改密码">修改密码</el-dropdown-item>
                      <el-dropdown-item command="授权角色">授权角色</el-dropdown-item>
                      <el-dropdown-item command="删除">删除</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </a>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="table-pagination">
          <!-- 分页 -->
          <el-pagination
            :page-sizes="[10, 20, 50, 100]"
            :page-size="params.size"
            :current-page="params.current"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <AddUser ref="AddUser" @reload="reload" />
    <AuthorizeRole ref="AuthorizeRole" @reload="reload" />
    <passwordUser ref="passwordUser" @reload="reload" />
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="20%"
    >
      <span>是否确定删除？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDel">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import AddUser from '@/components/system/addUser.vue';
import StructureList from '@/components/common/StructureList.vue';
import AuthorizeRole from '@/components/system/authorizeRole.vue';
import passwordUser from '@/components/system/passwordUser.vue';
import { GetUserList, DelUser } from '@/api/system';
export default {
  name: 'SystemManager',
  components: {
    AddUser,
    StructureList,
    AuthorizeRole,
    passwordUser
  },
  props: {},
  data() {
    return {
      searchForm: {},
      rules: {
        keyword: [
          { required: true, message: '关键词不能为空', trigger: 'blur' }
        ]
      },
      statusArr: [
        {
          label: '启用',
          value: 0
        },
        {
          label: '禁用',
          value: 1
        },
        {
          label: '未知',
          value: 2
        },
        {
          label: '测试状态1',
          value: 3
        }
      ],
      contentData: [],
      pageSize: 10,
      params: {
        current: 1,
        size: 10
      },
      total: 0,
      dialogVisible: false,
      id: ''
    }
  },
  computed: {},
  watch: {
  },
  created() {
    this.initData(this.params)
  },
  mounted() {},
  methods: {
    initData(params) {
      GetUserList(params).then(res => {
        if (res.success) {
          const { data } = res
          this.contentData = data.records
          this.total = data.total
        }
      })
    },
    nodeClick(data) {
      this.searchForm.orgId = data.id
      this.params.current = 1
      this.reload()
    },
    reload() {
      const requestBody = {
        ...this.searchForm,
        current: this.params.current,
        size: this.params.size
      }
      this.initData(requestBody)
    },
    search(form) {
      this.params = Object.assign(this.params, this.searchForm)
      this.initData(this.params)
    },
    reset(form) {
      this.$refs[form].resetFields();
    },
    edit(row) {
      this.$refs.AddUser.title = '编辑用户'
      this.$refs.AddUser.edit(row)
    },
    clickCommand(e, row) {
      this.id = row.id
      if (e === '授权角色') {
        this.$refs.AuthorizeRole.show(row.id)
      } else if (e === '修改密码') {
        this.$refs.passwordUser.show(row)
      } else if (e === '删除') {
        this.dialogVisible = true
        // DelUser({ ids: row.id }).then(res => {
        //   res.success && this.$message({
        //     message: res.msg,
        //     type: 'success'
        //   })
        //   this.reload()
        // })
      }
    },
    confirmDel() {
      DelUser({ ids: this.id }).then(res => {
        res.success && this.$message({
          message: res.msg,
          type: 'success'
        })
        this.dialogVisible = false
        this.reload()
      })
    },
    handleSelectionChange() {

    },
    handleSizeChange(e) {
      this.params.size = e;
      this.reload()
    },
    handleCurrentChange(e) {
      this.params.current = e
      this.reload()
    }
  }
}
</script>

<style scoped lang="scss">
.create-page {
  width: 100%;
  height: 100%;
  background-color: #fff;
  display: flex;
  .left-slide {
    width: 20%;
    height: 100%;
    border-right: 10px #f0f0f0 solid;
    text-align: center;
    padding-top: 10px;
    box-sizing: border-box;
  }
  .right-form-container {
    width: 80%;
    height: 100%;
    box-sizing: border-box;
    .header-box{
      width: 100%;
      background-color: #fff;
      padding: 10px 15px;
      ::v-deep .el-form-item{
        margin-right: 30px;
        .el-form-item__label{
          font-family: "Microsoft YaHei";
          color: #333
        }
      }
    }
    .table-container{
      width: 100%;
      border-top: 10px #f0f0f0 solid;
      flex: 1;
      margin-top: 10px;
      display: flex;
      flex-direction: column;
      .table-box {
        width: 100%;
        flex-grow: 1;
        overflow: hidden;
        overflow-y: auto;
        a, .el-dropdown-link{
          color: #409EFF;
        }
      }
      .table-pagination{
        height: 60px;
        width: 100%;
        text-align: right;
        background-color: #fff;
        margin-top: 10px;
        display: flex;
        align-items: center;
        justify-content: flex-end;
        ::v-deep .el-pagination{
          span, button, ul>li{
            height: 50px;
            line-height: 50px;
            font-size: 16px;
          }
        }
      }
    }
  }
}
</style>
